﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public partial class FormAutoRoungBeam : System.Windows.Forms.Form
	{
		public FormAutoRoungBeam(double minWidth, double minHeight, string eee)
		{
			this.InitializeComponent();
			this.minheight = minHeight;
			this.eee = eee;
			this.minwidth = minWidth;
			//base.Icon = Resource.YArchitechIcon;
		}

		public FormAutoRoungBeam(double minWidth, double minHeight, Pipe pipe)
		{
			this.InitializeComponent();
			this.minheight = minHeight;
			this.minwidth = minWidth;
			this.pipe = pipe;
			//base.Icon = Resource.YArchitechIcon;
		}

		private string isRemove(string name)
		{
			string result = "其他";
			XmlDocument xmlDocument = new XmlDocument();
			string filename = Path.Combine(Product.DataLocation, "User\\AutoRoundBeamUserSet.xml");
			xmlDocument.Load(filename);
			foreach (object obj in xmlDocument.SelectSingleNode("configuration").ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode.Attributes["pstname"].Value == name)
				{
					if (xmlNode.Attributes["roundorbeam"].Value == "上")
					{
						result = "上";
						break;
					}
					if (xmlNode.Attributes["roundorbeam"].Value == "下")
					{
						result = "下";
						break;
					}
					break;
				}
			}
			return result;
		}

		private void FormAutoRoungBeam_Load(object sender, EventArgs e)
		{
			base.CenterToScreen();
			if (this.pipe == null)
			{
				this.textBoxHigh.Text = this.userSetting.GetValue("Autopipearoundbeam", "length");
			}
			else
			{
                string name = this.pipe.GetParameter(Autodesk.Revit.DB.BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM).AsValueString();
				string a = this.isRemove(name);
				if (a == "上")
				{
					this.textBoxHigh.Text = Math.Abs(int.Parse(this.userSetting.GetValue("Autopipearoundbeam", "length"))).ToString();
				}
				else if (a == "下")
				{
					this.textBoxHigh.Text = (-Math.Abs(int.Parse(this.userSetting.GetValue("Autopipearoundbeam", "length")))).ToString();
				}
				else
				{
					this.textBoxHigh.Text = this.userSetting.GetValue("Autopipearoundbeam", "length");
				}
			}
			this.textBoxAngle.Text = this.userSetting.GetValue("Autopipearoundbeam", "angle");
			this.textBoxWitdh.Text = this.userSetting.GetValue("Autopipearoundbeam", "witdh");
			this.pictureBox1.Image = this.getImage("两侧升降");
			if (this.eee == "30")
			{
				this.SJrB30.Checked = true;
				return;
			}
			this.SJrB90.Checked = true;
		}

		private Image getImage(string me)
		{
			return Image.FromFile(Path.Combine(Product.ResLocation, me + ".png"));
		}

		private void buttonOK_Click(object sender, EventArgs e)
		{
			if (!Regex.IsMatch(this.textBoxHigh.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("升降高度必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.textBoxHigh.Focus();
				return;
			}
			if (!Regex.IsMatch(this.textBoxAngle.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("角度必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.textBoxAngle.Focus();
				return;
			}
			if (!Regex.IsMatch(this.textBoxWitdh.Text, "^[+-]?[0-9]+\\d*[.]?\\d*$"))
			{
				MessageBox.Show("间距必须为有效数字", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				this.textBoxWitdh.Focus();
				return;
			}
			this.offSet = Convert.ToDouble(this.textBoxHigh.Text.Trim());
			this.radioAngleChange();
			this.width = Convert.ToDouble(this.textBoxWitdh.Text.Trim());
			this.minheight *= Math.Sin(this.angle);
			if (Math.Abs(FormAutoRoungBeam.mmToFeet(this.offSet)) > this.minheight - 0.01)
			{
				if (FormAutoRoungBeam.mmToFeet(this.width) > this.minwidth - 0.01)
				{
					this.userSetting.WriteValue("Autopipearoundbeam", "length", this.textBoxHigh.Text);
					this.userSetting.WriteValue("Autopipearoundbeam", "angle", this.textBoxAngle.Text);
					this.userSetting.WriteValue("Autopipearoundbeam", "witdh", this.textBoxWitdh.Text);
					base.DialogResult = DialogResult.OK;
					return;
				}
				if (YJKMessageBox.Warning("两切割点间的距离输入过小，是否给出推荐值?") == DialogResult.OK)
				{
					this.textBoxWitdh.Text = (this.minwidth * 304.8).ToString();
					return;
				}
			}
			else if (YJKMessageBox.Warning("升降高度过低,是否给出推荐值?") == DialogResult.OK)
			{
				this.textBoxHigh.Text = (this.minheight * 304.8).ToString();
			}
		}

		private static double mmToFeet(double val)
		{
			return val / 304.8;
		}

		private void buttonESC_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void textBoxHigh_Validated(object sender, EventArgs e)
		{
			if (!new Regex("^[+-]?([1-9][0-9]*|0)(\\.[0-9]+)?$").IsMatch(this.textBoxHigh.Text))
			{
				YJKMessageBox.Show("高度请输入数字，可正负！");
				this.textBoxHigh.Focus();
				this.textBoxHigh.SelectAll();
				return;
			}
		}

		private void textBoxWitdh_Validated(object sender, EventArgs e)
		{
			if (!this.regex.IsMatch(this.textBoxWitdh.Text))
			{
				YJKMessageBox.Show("宽度请输入数字,必须为正！");
				this.textBoxWitdh.Focus();
				this.textBoxWitdh.SelectAll();
				return;
			}
		}

		private void radioAngleChange()
		{
			if (this.SJrB15.Checked)
			{
				this.angle = 0.26179938779914941;
				this.textBoxAngle.Enabled = false;
				return;
			}
			if (this.SJrB30.Checked)
			{
				this.angle = 0.52359877559829882;
				this.textBoxAngle.Enabled = false;
				return;
			}
			if (this.SJrB45.Checked)
			{
				this.angle = 0.78539816339744828;
				this.textBoxAngle.Enabled = false;
				return;
			}
			if (this.SJrB60.Checked)
			{
				this.angle = 1.0471975511965976;
				this.textBoxAngle.Enabled = false;
				return;
			}
			if (this.SJrB90.Checked)
			{
				this.angle = Math.PI*.5;
				this.textBoxAngle.Enabled = false;
				return;
			}
			if (this.radioButton3.Checked)
			{
				this.textBoxAngle.Enabled = true;
				if (this.textBoxAngle.Text != "")
				{
					this.angle = Convert.ToDouble(this.textBoxAngle.Text.Trim()) * Math.PI / 180.0;
				}
			}
		}

		private void SJrB15_CheckedChanged(object sender, EventArgs e)
		{
			this.radioAngleChange();
		}

		private void SJrB30_CheckedChanged(object sender, EventArgs e)
		{
			this.radioAngleChange();
		}

		private void SJrB45_CheckedChanged(object sender, EventArgs e)
		{
			this.radioAngleChange();
		}

		private void SJrB60_CheckedChanged(object sender, EventArgs e)
		{
			this.radioAngleChange();
		}

		private void SJrB90_CheckedChanged(object sender, EventArgs e)
		{
			this.radioAngleChange();
		}

		private void radioButton3_CheckedChanged(object sender, EventArgs e)
		{
			this.radioAngleChange();
		}

		private void textBoxAngle_Validated_1(object sender, EventArgs e)
		{
			if (!new Regex("^[0-9]+(.[0-9]{1,3})?$").IsMatch(this.textBoxAngle.Text))
			{
				YJKMessageBox.Show("角度请输入数字,且必须为正！");
				this.textBoxAngle.Focus();
				this.textBoxAngle.SelectAll();
				return;
			}
			if (this.regex.IsMatch(this.textBoxAngle.Text) && Convert.ToDouble(this.textBoxAngle.Text.Trim()) > 90.0)
			{
				YJKMessageBox.Show("角度不能大于90度！");
				this.textBoxAngle.Focus();
				this.textBoxAngle.SelectAll();
				return;
			}
		}

		public double offSet;

		public double angle = 1.0471975511965976;

		public double width;

		private Pipe pipe;

		private UserSetting userSetting = new UserSetting();

		private double minwidth;

		private double minheight;

		private string eee;

		private Regex regex = new Regex("^[0-9]+(.[0-9]{1,3})?$");
	}
}
